/*
 * @Author: 李石
 * @Date: 2024-05-21 14:14:29
 * @LastEditors: lishi
 * @LastEditTime: 2024-05-22 10:04:11
 * @Description: 
 * Copyright (c) 2024 by ${lishi0105@163.com}, All Rights Reserved. 
 */
#ifndef _NETWORK_UTILS_H_
#define _NETWORK_UTILS_H_

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    char name[16];
    char address[16];
    char netmask[16];
    char gateway[16];
    char mac[18];
    char dns[2][16];
    int dnsCount;
    int phy_connected;
    int iface_up;
    int speed;
    int ifDHCP;
} iface_info_t;

typedef struct {
    iface_info_t iface[16];
    int iface_count;
} host_net_info_t;

#ifndef __FILENAME__
#define __FILENAME__                                                         \
    (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 \
                                      : __FILE__)
#endif

#ifndef DBGI
#define DBGI(format...)                                       \
    do {                                                      \
        printf("\033[0;32m[%s:%d] ", __FILENAME__, __LINE__); \
        printf(format);                                       \
        printf("\033[0;39m");                                 \
    } while (0)
#endif

#ifndef DBGE
#define DBGE(format...)                                       \
    do {                                                      \
        printf("\033[0;31m[%s:%d] ", __FILENAME__, __LINE__); \
        printf(format);                                       \
        printf("\033[0;39m");                                 \
    } while (0)
#endif

int list_network_name(char (*iface)[32], int srcDepth);

int list_network_interfaces(host_net_info_t *host_net_info);

void outIfaceInfo(iface_info_t *iface_info);

int get_hostname(char *hostname, int srclen);

int find_free_port(int port_base, uint32_t range);

int get_local_ip(char *ip, size_t srclen);

#ifdef __cplusplus
}
#endif
#endif  // _NETWORK_UTILS_H_